using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;using THSERP.BusinessEntities;using THSERP.BaseProvider;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using THSLib;using THSERP.BaseProvider;

namespace THSERP.Modules.Report
{
    public partial class RP004 : ReportForm
    {
        public List<ARInvoiceItemsInfo> ARInvoiceItemBranchList = new THSList<ARInvoiceItemsInfo>();
        public THSList<ARInvoiceItemsInfo> ARInvoiceItemParentsList = new THSList<ARInvoiceItemsInfo>();

        public RP004()
        {
            InitializeComponent();
        }

        private void fld_btnApply_Click(object sender, EventArgs e)
        {
            //clear list ARInvoiceItemsBranch
            ARInvoiceItemBranchList.Clear();
            ARInvoiceItemParentsList.Clear();
            DateTime fromDate = Convert.ToDateTime(fld_dteSearchFromARInvoiceDate.EditValue);
            DateTime toDate = Convert.ToDateTime(fld_dteSearchToARInvoiceDate.EditValue);
            int iDepartmentID = Convert.ToInt32(fld_lkeFK_ICDepartmentID.EditValue);
            int iProductGroupID = Convert.ToInt32(fld_lkeFK_ICProductGroupID.EditValue);
            int iSupplierID = Convert.ToInt32(fld_lkeFK_APSupplierID.EditValue);
            String strQuery = ((ReportModule)Module).GenerateInvoiceItemSearchQuery(fromDate, toDate, iDepartmentID, iProductGroupID, iSupplierID, 0, "ii.FK_ICDepartmentID, ii.FK_ICProductGroupID, ii.FK_ICProductID, ii.FK_ICProductAttributeID, i.FK_BRBranchID,ii.ARInvoiceItemProductUnitPrice");
            DataSet dsInvoiceItems = new ARInvoiceItemsController().GetDataSet(strQuery);
            THSList<ARInvoiceItemsInfo> lstARInvoiceItems = new THSList<ARInvoiceItemsInfo>();
            foreach (DataRow row in dsInvoiceItems.Tables[0].Rows)
            {
                ARInvoiceItemsInfo objARInvoiceItemsInfo = (ARInvoiceItemsInfo)new ARInvoiceItemsController().GetObjectFromDataRow(row);
                if (objARInvoiceItemsInfo != null)
                {
                    objARInvoiceItemsInfo.BRBranchName = new BRBranchsController().GetObjectNameByID(Convert.ToInt32(row["FK_BRBranchID"].ToString()));
                    objARInvoiceItemsInfo.ARInvoiceItemProductAttribute = new ICProductAttributesController().GetAttributeValueByProductAttributeID(objARInvoiceItemsInfo.FK_ICProductAttributeID);
                    objARInvoiceItemsInfo.ARInvoiceItemProductDesc = GetProductDescription(objARInvoiceItemsInfo.FK_ICProductID);
                    ARInvoiceItemParentsList.Add(objARInvoiceItemsInfo);
                    //hieu.nguyen change 10/08/2011
                    //if (!lstARInvoiceItems.Exists("FK_ICProductAttributeID", objARInvoiceItemsInfo.FK_ICProductAttributeID))
                    if (!lstARInvoiceItems.Exists("FK_ICProductID", objARInvoiceItemsInfo.FK_ICProductID))
                        lstARInvoiceItems.Add(objARInvoiceItemsInfo);

                }
            }

            fld_dgcARRP004InvoiceItems.DataSource = lstARInvoiceItems;
            fld_dgcARRP004InvoiceItems.RefreshDataSource();
            fld_dgcARRP004InvoiceItemBranchs.DataSource = ARInvoiceItemBranchList;
            fld_dgcARRP004InvoiceItemBranchs.RefreshDataSource();
        }

        private void fld_btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void RP004_Load(object sender, EventArgs e)
        {
            //initalize search controller 
            fld_dteSearchFromARInvoiceDate.DateTime = THSApp.WorkingDate;
            fld_dteSearchToARInvoiceDate.DateTime = THSApp.WorkingDate;
            fld_dgcARRP004InvoiceItems.Screen = this;
            fld_dgcARRP004InvoiceItems.InitializeControl();
            fld_dgcARRP004InvoiceItemBranchs.Screen = this;
            fld_dgcARRP004InvoiceItemBranchs.InitializeControl();

            fld_btnApply_Click(new object(), new EventArgs());
        }

        private void fld_btnBrown_Click(object sender, EventArgs e)
        {
            ShowCategoryTree();
        }

        /// <summary>
        /// show treelist Category
        /// </summary>
        /// <remarks>HieuNH_15/05/2010</remarks>
        /// 
        public void ShowCategoryTree()
        {
            int iDepartmentID = 0;
            iDepartmentID = Convert.ToInt32(fld_lkeFK_ICDepartmentID.EditValue);
            guiProductGroupTree guiProductGroupTree = new guiProductGroupTree();
            guiProductGroupTree.Module = this.Module;
            guiProductGroupTree.ICDepartmentID = iDepartmentID;

            if (guiProductGroupTree.ShowDialog() == DialogResult.OK)
            {

                ICProductGroupsInfo objProductGroupsInfo = (ICProductGroupsInfo)guiProductGroupTree.TreeList.GetSelectedObject();
                if (objProductGroupsInfo != null)
                {
                    fld_lkeFK_ICProductGroupID.EditValue = objProductGroupsInfo.ICProductGroupID;
                }
                fld_lkeFK_ICDepartmentID.EditValue = guiProductGroupTree.ICDepartmentID;
            }
        }

        private String GetProductDescription(int iProductID)
        {
            ICProductsInfo objICProductsInfo = (ICProductsInfo)new ICProductsController().GetObjectByID(iProductID);
            if (objICProductsInfo != null)
                return objICProductsInfo.ICProductDesc;
            return String.Empty;

        }
        public void GetInvoiceItemBranchByAttribute(int iProductID)
        {
            ARInvoiceItemBranchList = new List<ARInvoiceItemsInfo>();
            foreach (ARInvoiceItemsInfo objARInvoiceIntemsInfo in ARInvoiceItemParentsList)
            {
                if (objARInvoiceIntemsInfo.FK_ICProductID == iProductID)
                {
                    ARInvoiceItemBranchList.Add(objARInvoiceIntemsInfo);
                }
            }
            fld_dgcARRP004InvoiceItemBranchs.DataSource = ARInvoiceItemBranchList;
            fld_dgcARRP004InvoiceItemBranchs.RefreshDataSource();
        }

        public void InvalidateGridInvoiceItemsBranch()
        {
            fld_dgcARRP004InvoiceItemBranchs.DataSource = ARInvoiceItemBranchList;
            fld_dgcARRP004InvoiceItemBranchs.RefreshDataSource();
        }
    }
}